package com.ruijie.acl.web.config;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.jboss.seam.jsf.ListDataModel;

import com.ruijie.acl.AccessGroup;
import com.ruijie.acl.service.AccessGroupService;
import com.ruijie.acl.utils.AclUtil;

/**
 * <p>
 * Title: ACL组选择器
 * </p>
 * <p>
 * Description: 组件的生命周期即由客户端以编程的方式管理
 * </p>
 * <p>
 * Copyright: Copyright (c) 2009
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time: 2009-09-22
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p>
 * 
 * @author ASAI
 */
public class AccessGroupSelector {

	private AccessGroupService accessGroupSelectorService;

	private AccessGroupAvailableSelector availableSelector;

	private AccessGroupSelectedSelector selectedSelector;

	/**
	 * 客户端手动实例化selector时的实例化构造函数
	 * 
	 * @param deviceSelectorService
	 * @param deviceModelService
	 */
	public AccessGroupSelector(AccessGroupService accessGroupSelectorService) {
		this.accessGroupSelectorService = accessGroupSelectorService;

		init();
	}

	/**
	 * <p>
	 * Description: ACL组选择器的初始化方法
	 * </p>
	 * <ui>
	 * <li>若采用由spring实例化selector，则本方法由spring回调</li>
	 * <li> 若采用手动实例化selector，则本方法由构造函数调用</li>
	 * </ui>
	 * <p>
	 * Create Time: 2009-09-22
	 * </p>
	 * 
	 * @author ASAI
	 */
	private void init() {
		availableSelector = new AccessGroupAvailableSelector(this,
				accessGroupSelectorService);
		selectedSelector = new AccessGroupSelectedSelector(
				accessGroupSelectorService);

		availableSelector.setSelectedSelector(selectedSelector);
		selectedSelector.setAvailableSelector(availableSelector);
	}

	/**
	 * <p>
	 * Description: 可选设备DataModel
	 * </p>
	 * <p>
	 * Create Time: 2009-09-22
	 * </p>
	 * 
	 * @author ASAI
	 * @return
	 */
	public ListDataModel getAvailableDataModel() {
		return availableSelector.getAvailableDataModel();
	}

	/**
	 * <p>
	 * Description: 可选设备DataModel, 根据criterion过滤
	 * </p>
	 * <p>
	 * Create Time: 2009-09-22
	 * </p>
	 * 
	 * @author ASAI
	 * @return
	 */
	public void setAvailDeviceCriterion(Criterion criterion) {
		availableSelector.setCriterion(criterion);
	}

	/**
	 * <p>
	 * Description: 可选设备DataModel, 根据sql语句过滤, 仅用于SelectAll方法
	 * </p>
	 * <p>
	 * Create Time: 2009-09-22
	 * </p>
	 * 
	 * @author ASAI
	 * @return
	 */
	public void setAvailDeviceCriterionForSelectAll(String sql) {
		availableSelector.setSqlCriteriaForSelectAll(sql);
	}

	/**
	 * <p>
	 * Description: 已选设备DataModel
	 * </p>
	 * <p>
	 * Create Time: 2009-09-22
	 * </p>
	 * 
	 * @author ASAI
	 * @return
	 */
	public ListDataModel getSelectedDataModel() {
		return selectedSelector.getSelectedDataModel();
	}

	/**
	 * <p>
	 * Description: 所有已经设备DataModel,不分页
	 * </p>
	 * <p>
	 * Create Time: 2009-09-22
	 * </p>
	 * 
	 * @author ASAI
	 * @return
	 */
	public ListDataModel getSelectedDataModelNoPage() {
		return selectedSelector.getSelectedDataModelNoPage();
	}

	/**
	 * <p>
	 * Description: 获取所有已经选择的设备
	 * </p>
	 * <p>
	 * Create Time: 2009-09-22
	 * </p>
	 * 
	 * @author ASAI
	 * @return
	 */
	public List<AccessGroup> getAllSelectedDevices() {
		return selectedSelector.getAllSelectedDevices();
	}

	public void setDeviceSelectorService(
			AccessGroupService accessGroupSelectorService) {
		this.accessGroupSelectorService = accessGroupSelectorService;
	}

	public AccessGroupAvailableSelector getAvailableSelector() {
		return availableSelector;
	}

	public void setAvailableSelector(
			AccessGroupAvailableSelector availableSelector) {
		this.availableSelector = availableSelector;
	}

	public AccessGroupSelectedSelector getSelectedSelector() {
		return selectedSelector;
	}

	public void setSelectedSelector(AccessGroupSelectedSelector selectedSelector) {
		this.selectedSelector = selectedSelector;
	}

	/**
	 * <p>
	 * Description: 同一个Selector实例反复使用时，需要显式清理数据
	 * </p>
	 * <p>
	 * Create Time: 2009-09-22
	 * </p>
	 * 
	 * @author ASAI
	 */
	public void clearAllData() {
		// deviceSelectorService.clearData(uuid);
		init();
	}

	public String convertGroupList2String(){
		return convertGroupList2String(this.getSelectedSelector().getSelectedAccessGroupList());
	}	
	public String convertGroupList2String(List<Long> accessGroupList){
		String result = "";
		if (accessGroupList != null){
			for(Long accessGroup :accessGroupList){
				result += "," + accessGroup.toString();
			}
			if (result.length() > 0){
				result = result.substring(1);
			}
		}
		return result;
	}
	
	public List<Long> convertString2GroupList(String accessGroupString){
		List<Long> idList = new ArrayList<Long>();
		if (!AclUtil.empty(accessGroupString)){
			String[] arrIds = accessGroupString.split(",");
			for(String id : arrIds){
				if (!AclUtil.empty(id)){
					idList.add(Long.parseLong(id));
				}
			}
		}
		return idList;
	}
	
	/**
	 * <p>
	 * Description: 刷新数据，保持与数据库一致，在计划管理中， 由于底层在比如更新时会绕过selecotr直接把数据放入临时表，
	 * 从而让slector与临时表的数据不一致，所以客户端使用时， 请在需要的地方手动调用本方法<br/>
	 * </p>
	 * <p>
	 * Create Time: 2009-09-22
	 * </p>
	 * 
	 * @author ASAI
	 */
	public void refresh() {
		selectedSelector.refresh();
		availableSelector.refresh();
	}

}
